/**
 * 关于商品分类全局组件的小仓库
 */
import { defineStore } from 'pinia'
import type { CategoryResponseData } from '@/api/product/attr/type'
import type { categoryState } from '@/store/modules/types/type'
// 引入分类的接口方法
import {
  reqGetCategory1,
  reqGetCategory2,
  reqGetCategory3,
} from '@/api/product/attr'

let useCategoryStore = defineStore('category', {
  state: (): categoryState => {
    return {
      // 定义一级分类数据
      category1Arr: [],
      category1Id: '',
      // 定义二级分类数据
      category2Arr: [],
      category2Id: '',
      // 定义三级分类数据
      category3Arr: [],
      category3Id: '',
    }
  },

  getters: {},

  actions: {
    // 获取一级分类
    async getCategory1() {
      let result: CategoryResponseData = await reqGetCategory1()
      if (result.code === 200) {
        this.category1Arr = result.data
      }
    },

    // 获取二级分类
    async getCategory2() {
      let result: CategoryResponseData = await reqGetCategory2(this.category1Id)
      if (result.code === 200) {
        this.category2Arr = result.data
      }
    },

    // 获取三级分类
    async getCategory3() {
      let result: CategoryResponseData = await reqGetCategory3(this.category2Id)
      if (result.code === 200) {
        this.category3Arr = result.data
      }
    },
  },
})

export default useCategoryStore
